package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.utils.EventListenerUtil;
    import components.utils.StringUtil;
    import components.utils.URLRequestUtil;

    import flash.display.Loader;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;

    [Event(name = "complete", type = "flash.events.Event")]
    public class BaseImage extends UIComponent
    {
        private var _sp:Sprite;

        private var _scaleContent:Boolean=true;

        private var _source:String = "";

        private var _loader:Loader;

        private var _mask:Shape;

        private var _isLoadComplete:Boolean;

        private var _selected:Boolean;

        private var _value:String;

        private var _errorHide:Boolean = false;

        public function BaseImage()
        {
            super();
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get errorImage():Sprite
        {
            return getChildByName("errorImageSkin") as Sprite;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
                _sp = new Sprite();
                addChild(_sp);

                _loader = new Loader();
                cleanObjList.push(_loader.contentLoaderInfo);
                EventListenerUtil.addEventListener(_loader.contentLoaderInfo, Event.COMPLETE, loaderCompleteHandler);
                EventListenerUtil.addEventListener(_loader.contentLoaderInfo, IOErrorEvent.IO_ERROR, loaderIoErrorHandler);
                _sp.addChild(_loader);

                _mask = new Shape();
                _mask.graphics.beginFill(0x000000);
                _mask.graphics.drawRect(0, 0, 1, 1);
                _mask.graphics.endFill();

                _sp.addChild(_mask);
                _sp.mask = _mask;
            }

			if(_isLoadComplete)
			{
	            if (!_scaleContent)
	            {
	                _width = _loader.width;
	                _height = _loader.height;
	            }
	            else
	            {
	                var scaleW:Number = width / _loader.width;
	                var scaleH:Number = height / _loader.height;
	                var scale:Number = Math.min(scaleW, scaleH);
	                _loader.width *= scale;
	                _loader.height *= scale;
	            }
			}
			
            errorImage.width = width;
            errorImage.height = height;

            checkErrorHide();
        }

        public function checkErrorHide():void
        {
            if (errorHide && errorImage.visible)
                visible = false;
            else
                visible = true;
        }

        protected override function measureSizes():Boolean
        {
            var change:Boolean = super.measureSizes();
            if (change)
            {
                _mask.width = width;
                _mask.height = height;
            }

            return change;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        [Inspectable(defaultValue = "")]
        public function get source():String
        {
            return _source;
        }

        public function set source(value:String):void
        {
            if (source == value)
                return;

            closeLoader();

            _source = value;

            if (!StringUtil.isEmpty(source))
            {
                errorImage.visible = false;
                _loader.visible = true;
                _loader.load(URLRequestUtil.getNoCacheURLRequest(source));
            }

            checkErrorHide();
        }

        private function closeLoader():void
        {
            try
            {
                if (_loader.contentLoaderInfo.bytesLoaded != _loader.contentLoaderInfo.bytesTotal)
                    _loader.close();
            }
            catch (error:Error)
            {
                trace(error);
            }
            finally
            {
                _source = null;
                _isLoadComplete = false;
                _loader.unload();
                _loader.visible = false;
                errorImage.visible = true;
            }
        }


        [Inspectable(defaultValue = "true")]
        public function get scaleContent():Boolean
        {
            return _scaleContent;
        }

        public function set scaleContent(value:Boolean):void
        {
            _scaleContent = value;

            invalidate();
        }

        [Inspectable(defaultValue = "")]
        public function get value():String
        {
            return _value;
        }

        public function set value(value:String):void
        {
            _value = value;
        }

        [Inspectable(defaultValue = "false")]
        public function get errorHide():Boolean
        {
            return _errorHide;
        }

        public function set errorHide(value:Boolean):void
        {
            _errorHide = value;

            invalidate();
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        protected function loaderCompleteHandler(event:Event):void
        {
			if(isDestory)
				return ;
            if (source)
            {
                _isLoadComplete = true;
                errorImage.visible = false;
				
				if (!_scaleContent)
				{
					_width = _loader.width;
					_height = _loader.height;
				}
				
                dispatchEvent(new Event(Event.COMPLETE));
                invalidate();
            }
            else
                closeLoader();
        }

        public override function destroy():void
        {
            super.destroy();

            _source = null;
            if (_loader)
                _loader.unloadAndStop();

            _loader = null;
            _mask = null;
        }

        public function get selected():Boolean
        {
            return _selected;
        }

        public function set selected(value:Boolean):void
        {
            if (selected == value)
                return;

            _selected = value;
            if (selected)
                filters = [ glowFilter ];
            else
                filters = [];
        }

        protected function loaderIoErrorHandler(event:IOErrorEvent):void
        {
            errorImage.visible = true;
            checkErrorHide();
            trace(event.text);
        }

    }
}
